import json

from bs4 import BeautifulSoup
from loguru import logger


class MajorParser():
    def __init__(self):
        super().__init__()

    def parse_major_list(self, html):
        soup = BeautifulSoup(html, "lxml")
        div_tags = soup.find_all("div", {"class": "marlist"})
        major_list = []
        for div in div_tags:
            a_tags = div.find_all("a", {"class": "list-item"})
            for a_tag in a_tags:
                major_list.append({
                    "href": a_tag.attrs["href"].replace("/majors", ""),
                    "text": a_tag.text,
                })
                logger.info(a_tag)
        return major_list

    def parse_major_detal(self, html, pphtml, major_name, major_code):
        soup = BeautifulSoup(html, "lxml")
        navs = soup.find("div", {"class", "zhuanyedsc"}).find("li", {"class", "navs"}).find_all("span")
        subject_name = navs[0].text
        subject_code = major_code[0:2]
        major_category_name = navs[1].text
        major_category_code = major_code[0:4]
        gailan = soup.find("div", {"class": "gailan"})
        desc_arr = gailan.find_all("div", {"class": "desc"})
        level = desc_arr[0].find("p").text
        age_limit = desc_arr[1].find("p").text
        degree = desc_arr[2].find("p").text
        wl_ratio = desc_arr[3].find("p").text
        sex_ratio = desc_arr[4].find("p").text
        salary_text = desc_arr[5].find("p").text
        salary = 0 if "--" in salary_text else salary_text.replace("元", "")

        zzyjs = soup.find("div", {"class": "zzyjs"})
        zzyjs_html = str(zzyjs.find("div", {"class": "introduce"}))

        zzykc = soup.find("div", {"class": "zzykc"})
        zzykc_content = zzykc.find("div", {"class": "introduce"}).text

        zzyjyfx = soup.find("div", {"class": "zzyjyfx"})
        zzyjyfx_content = zzyjyfx.find("div", {"class": "introduce"}).text

        # zzyjyfx = soup.find("div", {"class": "zzyjyfx"})
        # zzyjyfx_content = zzyjyfx.find("div", {"class": "introduce"}).text

        p_soup = BeautifulSoup(pphtml, "lxml")
        p_soup_div = p_soup.find_all("div", {"class": "xiangqingw layui-col-xs9 detail-info"})
        xiangqingw_html = str(p_soup_div[-1].find("div", {"class", "introduce"}))

        return {
            "major_name": major_name,
            "major_code": major_code,
            "major_category_name": major_category_name,
            "major_category_code": major_category_code,
            "subject_name": subject_name,
            "subject_code": subject_code,
            "education_level": level,
            "age_limit": age_limit,
            "major_degree": degree,
            "major_hot_level": 0,
            "wl_rate": wl_ratio,
            "sex_rate": sex_ratio,
            "salary": salary,
            "major_detail_info": zzyjs_html,
            "major_course": zzykc_content,
            "employ_direction": zzyjyfx_content,
            "graduation_trace": xiangqingw_html,
        }
